<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//站点相关信息
class Sites extends MY_Controller {
    public function __construct(){
        parent::__construct();
        $this->sess_checked();
        $this->load->model('sites_m');
        $this->load->model('column_m');
    }
    public function lists(){
        //echo $_SERVER['DOCUMENT_ROOT'];exit;
        $selectedMoudelstr = getConfig('selectedMoudels');
        $selectedMoudels = json_decode($selectedMoudelstr,true);
		//可选择模板
        $enameMoudels=json_decode(getConfig('sitetpl'),true);
        $arr=$this->sites_m->ajaxall();
        foreach ($arr['list'] as $k=>$v)
        {
			if(isset($selectedMoudels[$v['id']])){//已经存在模版
				$arr['list'][$k]['selectedMoudel'] = $selectedMoudels[$v['id']];
			}else{//未选中模版
				$arr['list'][$k]['selectedMoudel'] = '';
			}
        }
        $this->assign('list',$arr['list']);
        if(isset($_POST['ajaxs'])){
            $htmlcontent=$this->fetch('sites/siteslist.html');
            $arr['list']=$htmlcontent;
            ajax_return(1,'',$arr);
        }
        $this->assign('pageshow',$arr['pageshow']);
        $this->assign('enameMoudels', $enameMoudels);
        $this->display('sites/index.html');
    }
    private function site_data($data){
        if(trim($data['name'])==''){ajax_return(0,'网站名称,不能为空');}
        if(!check_regex($data['ename'],'isnozh')){ajax_return(0,'英文名称不匹配');}
        if(trim($data['domain'])==''){ajax_return(0,'访问域名,不能为空');}
        $data['addtime']=time();
        $data['description']=str_safe($data['description']);
        $data['name']=str_safe($data['name']);
        $data['logo']=str_safe($data['logo']);
        return $data;
    }
    //添加站点
    public function add(){
        if(isset($_POST['ajaxs'])){
            $data=form_ajaxs($_POST['formjquery']);
            $data=$this->site_data($data);
            //判断站点中文名称是否已经存在
            //判断站点中文名称是否已经存在
            $where['name']=array('=',$data['name']);
            $where['ename']=array('=',$data['ename'],'||');
            if($list=$this->sites_m->sel($where,'id,name,ename')){
                if($list['name']==$data['name']){
                    ajax_return(0,'已经存在网站名称');
                }
                if($list['ename']==$data['ename']){
                    ajax_return(0,'已经存在英文名称');
                }
            }
            if($addid=$this->sites_m->add($data)){
                $selectedMoudelstr = getFileConfig('moudleconfig')['selectedMoudels'];
                $selectedMoudels = json_decode($selectedMoudelstr,true);
                $selectedMoudels[intval($addid)] = 'default';
                $selectedMoudelsNew['selectedMoudels'] = json_encode($selectedMoudels);
                setFileConfig('moudleconfig',$selectedMoudelsNew);
                $this->adminuserRecord_m->add(1,1,'站点id为'.$addid);
                ajax_return(1,'添加站点成功');
            }else{
                $this->adminuserRecord_m->add(1,2,'站点');
                ajax_return(0,'添加站点失败');
            }
        }else{
            $this->display('sites/add.html');
        }
    }
    //编辑站点
    public function edit(){
        if(isset($_POST['ajaxs'])){//
            $data=form_ajaxs($_POST['formjquery']);
            $data=$this->site_data($data);
            $where['id']=array('=',$data['id']);
            //判断站点中文名称是否已经存在
            $where_isexits='(id!='.$data['id'].') && (name="'.$data['name'].'" || ename="'.$data['ename'].'")';
            if($list=$this->sites_m->sel_complex($where_isexits,'id,name,ename')){
                if($list['name']==$data['name']){
                    ajax_return(0,'已经存在网站名称');
                }
                if($list['ename']==$data['ename']){
                    ajax_return(0,'已经存在英文名称');
                }
            }
            if($this->sites_m->edit($where,$data)===false){
                $this->adminuserRecord_m->add(2,2,'站点id为'.$data['id']);
                ajax_return(0,'编辑失败');
            }
            $this->adminuserRecord_m->add(2,1,'站点id为'.$data['id']);
            ajax_return(1,'编辑成功');
        }else{

            if(isset($_GET['id'])){
                $where['id']=array('=',$_GET['id']);
                $list=$this->sites_m->sel($where);
                $this->assign('list',$list);
                $this->display('sites/edit.html');
            }
        }
    }
    //删除站点
    public function del(){
        if(isset($_POST['id'])){
            $id=$_POST['id'];
            //判断站点下是否有栏目
            $where_c['sitesid']=array('=',$id);
            if($this->column_m->sel($where_c,'sitesid,colid')){
                ajax_return(0,'站点下存在栏目,请删除栏目');
            }

            $where['id']=array('=',$id);
        }else{
            ajax_return(0,'参数出错');
        }
        if($this->sites_m->del($where)===false){
            $this->adminuserRecord_m->add(3,2,'站点id为'.$id);
            ajax_return(0,'删除失败');
        }
        $this->adminuserRecord_m->add(3,1,'站点id为'.$id);
        ajax_return(1,'删除成功');
    }
    //选择模板
    public function selMoudel()
    {
        if(isset($_POST['ajaxs'])){
            $selectedMoudelstr = getConfig('selectedMoudels');
            $selectedMoudels = json_decode($selectedMoudelstr,true);
            $data = $this->input->post();
            if(!isset($data['ename']) || !isset($data['id']) || !isset($data['modoule']))
            {
                ajax_return(0, '参数错误');
            }
            if($selectedMoudels[intval($data['id'])] == $data['modoule'])
            {
                ajax_return(0, '未选择模板');
            }
            $siteinfo = $this->sites_m->sel(array('ismain'=>array('=',1)));
            $url = 'http://'.$siteinfo['domain'].'/Api/siteApi';
            $post_data['ename']    = $data['ename'];
            $post_data['sitetpl']   = $data['modoule'];
            $res = $this->request_post($url,$post_data);
            $result = json_decode($res, true);
            if($result['status']){
                $selectedMoudels[intval($data['id'])] = $data['modoule'];
                $selectedMoudelsNew['selectedMoudels'] = json_encode($selectedMoudels);
                setFileConfig('moudleconfig',$selectedMoudelsNew);
                $this->adminuserRecord_m->add(2,1,'站点id为'.$data['id']);
                ajax_return(1,$result['msg']);
            }else{
                $this->adminuserRecord_m->add(2,2,'站点id为'.$data['id']);
                ajax_return(0,$result['msg']);
            }
        }
    }
}
